#!/bin/sh

e2e_exclusive_lock() {
  true
}

e2e_test_install() {
  k8s_cleanup_namespace "$OPERATOR_NAMESPACE"
  k8s_async_cleanup

  install_cert_manager

  kubectl create namespace "$OPERATOR_NAMESPACE"

  install_operator_only \
    --set cert.certManager.autoConfigure=true

  kubectl create namespace "$CLUSTER_NAMESPACE"
}

e2e_test_uninstall() {
  uninstall_cert_manager
}

e2e_test() {
  run_test "Check that a cluster can be created after integrating with cert manager" check_cluster_can_be_created_after_cert_manager_integration
}

check_cluster_can_be_created_after_cert_manager_integration() {
  local RESULT EXIT_CODE
  NODE_LABEL_KEY="$(random_string)"
  NODE_LABEL_VALUE="$(random_string)"

  try_function create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" 1 \
    --set "cluster.pods.scheduling.nodeSelector.$NODE_LABEL_KEY=$NODE_LABEL_VALUE"
  if "$RESULT"
  then
    success "Cluster can be installed"
  else
    fail "Cluster can not be installed"
  fi
  helm_cleanup_chart "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
  k8s_async_cleanup_namespace "$CLUSTER_NAMESPACE"
}
